package com.bluesky.web;

import com.bluesky.common.dto.LoginDTO;
import com.bluesky.common.dto.UserDTO;
import com.bluesky.common.error.ErrorEnum;
import com.bluesky.common.log.MethodLog;
import com.bluesky.common.response.Result;
import com.bluesky.common.util.JwtUtils;
import com.bluesky.dao.dataobject.UserPO;
import com.bluesky.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Slf4j
@RestController
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户",notes = "根据姓名查询用户")
    @PostMapping("/user/query")
    //public Result<UserDTO> queryUserByName(@PathVariable String name){因为@PathVariable这个注解不能用来aop，所以就先注释了
    public Result<UserDTO> queryUserByName(@RequestBody UserQueryRequest request){
        UserDTO user=userService.queryUserByName(request.getName());
        if(Objects.isNull(user)){
            return Result.Result(ErrorEnum.DATA_NOT_FOUND,null);
        }
        return Result.Result(ErrorEnum.SUCCESS,user);
    }

    @ApiOperation(value="新增用户",notes="根据用户名和ID添加新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "userName",value = "用户名",required = true),
            @ApiImplicitParam(paramType = "query",name = "userID",value = "用户ID",required = true)
    })
//    @MethodLog
    @PostMapping("/user")
    public String addUser(@RequestParam String userName,@RequestParam Integer userID){
        userService.createUser(userName);
        return "新增用户:"+userName+", ID:"+userID;
    }

    @ApiOperation(value = "修改用户信息",notes="更新用户信息")
    @PutMapping("/user")
    public String updateUser(@RequestBody UserDTO userDTO){return userDTO.toString();}

    @ApiOperation(value = "查询所有用户",notes = "查询所有用户")
    @GetMapping("/user/all")
    public Result<List<UserDTO>> queryAllUser(){
        List<UserDTO> users=userService.queryAll();
        if(CollectionUtils.isEmpty(users)){
            return Result.Result(ErrorEnum.DATA_NOT_FOUND,null);
        }
        return Result.Result(ErrorEnum.SUCCESS,users);
    }
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDTO loginDTO){
        log.info("员工登录: {}",loginDTO.toString());
        LoginDTO e=userService.login(loginDTO);

        //登录成功，生成令牌，下发令牌

        if(Objects.isNull(e)){


            return Result.Result(ErrorEnum.DATA_ERROR,null);
        }else{
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getName());
//            claims.put("password",e.getPassword());

            String jwt = JwtUtils.generateJwt(claims);//jwt包含了登录员工的信息和密码

            return Result.Result(ErrorEnum.SUCCESS,jwt);
        }
    }
}
